package com.example.springboottestgit.controller;

import com.example.springboottestgit.bean.CommonException;
import com.example.springboottestgit.bean.ResultMsg;
import com.example.springboottestgit.bean.dto.EconomicPolicyDTO;
import com.example.springboottestgit.bean.vo.EconomicPolicyVO;
import com.example.springboottestgit.enums.ExceptionCodeEnum;
import com.example.springboottestgit.service.EconomicPolicyModelService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @description:经济政策大模型
 * @author: ZZZ
 */
@RestController
@RequestMapping("/economic/policy")
public class EconomicPolicyModelController {
    @Autowired
    private EconomicPolicyModelService economicPolicyModelService;

    /**
     * 经济政策 提问
     *
     * @return
     */
    @PostMapping("/ask")
    public ResultMsg<EconomicPolicyVO> economicPolicyAsk(@RequestBody EconomicPolicyDTO dto) throws InvocationTargetException, IllegalAccessException {

        if (!StringUtils.hasText(dto.getQuestion())) {
            throw new CommonException(ExceptionCodeEnum.QUERY_NO_RESULT.code, "question不能为空");
        }

        if (!StringUtils.hasText(dto.getType()) || !dto.getType().matches("[12]")) {
            throw new CommonException(ExceptionCodeEnum.QUERY_NO_RESULT.code, "type 错误");
        }

        //if ("1".equals(dto.getType())) {
            //if (!dto.getStartYear().matches("\\d{4}") || !dto.getEndYear().matches("\\d{4}")) {
                //throw new CommonException(ExceptionCodeEnum.PARAMETER_ERROR.code, "startYear or endYear 格式错误");
            //}
        //}


        long id = economicPolicyModelService.save(dto);
        dto.setId(id);
        economicPolicyModelService.economicPolicyAsk(dto);
        EconomicPolicyVO vo = new EconomicPolicyVO();
        BeanUtils.copyProperties(vo, dto);
        vo.setStatus(0);
        return ResultMsg.success(vo);

    }

    /**
     * 经济政策 提问结果查询
     *
     * @param id
     * @return
     */

    @GetMapping("/history/query")
    public ResultMsg<EconomicPolicyVO> economicPolicyQuery(long id) {
        EconomicPolicyVO economicPolicyVO = economicPolicyModelService.economicPolicyQuery(id);
        return ResultMsg.success(economicPolicyVO);
    }


    /**
     * 经济政策 提问结果查询
     *
     * @return
     */

    @GetMapping("/history/query/list")
    public ResultMsg<List<EconomicPolicyVO>> economicPolicyQueryList(long id) {
        List<EconomicPolicyVO> list = economicPolicyModelService.economicPolicyQueryList(id);
        return ResultMsg.success(list);
    }

}
